#ifndef __PLATFORM_LIB_STRING_H__
#define __PLATFORM_LIB_STRING_H__
#ifdef __cplusplus
extern "C"
{
#endif

#include "pf_common.h"

/******************************************************************************************************/

/*********************************************************************************************
Function Name: pf_str_2_bin
Description  : Convert string to binary
Inputs       : int32_t bin_size          : binary buffer max size,Must > 0.
               const char* str           : string to be convert. Must be not NULL.
Outputs      : char* void                : binary buffer. Must be not NULL.
               return value              : 0   : Success
                                          <0   : Fail. See errno.
ErrorCodes   : PF_EINVAL                 : 1. bin_size<=0; 
                                           2. str is "" ; 
                                           3. string include invalid charactors 
                                              not in "0-9" "a-f" "A-F" ;
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
lanqj                  2011-6-24                     create
**********************************************************************************************/
extern int32_t pf_str_2_hex(void* bin, int32_t bin_size, const char* str);

/*********************************************************************************************
Function Name: pf_hex_2_str
Description  : Convert binary to string with base.
Inputs       : int32_t str_len           : the length of the string after converted. Must >= bin_size*2+1.
               const void* bin           : binary bufer. Must not be NULL.
               int32_t bin_size          : binary buffer size. Must > 0.
               int32_t lowercase         : the lowercase of the string to be output.
Outputs      : char* str                 : the buffer of the converted string,it must be no NULL.
               return value              :  0  : Success.
                                           <0  : Fail.
ErrorCodes   : PF_EINVAL                 : 1. bin_size <= 0 or str_len < bin_size*2+1;
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
lanqj                  2011-6-24                     create
**********************************************************************************************/
extern int32_t pf_hex_2_str(char* str, 
                            int32_t str_len, 
                            const void* bin, 
                            int32_t bin_size, 
                            int32_t lowercase);     


/*********************************************************************************************
Function Name: pf_int_2_str
Description  : Convert integer to string with base
Inputs       : int32_t str_len              : the max string length to be output.
               int64_t input                : the integer to be converted.
               int32_t lowercase            : the lowercase of the string to be output.
                                              0 means lower.
               int32_t base                 : it supports 0,2-36.
Outputs      : char* str                    : string buffer,it must be not NULL.
               return value                 : >= 0  : the real byte number of string buffer.
                                              <  0  : Fail.
ErrorCodes   : PF_EINVAL                    : 1. base is not 0,2-36; 
                                              2. str_len <= 0; 
                                              3. str_len is not long enough to store 
                                                 the entire integer after convert
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
lanqj                   2011-6-24                  create
*********************************************************************************************/
extern int32_t pf_int_2_str(char* str,
                            int32_t str_len,
							int64_t input,
                            int32_t lowercase, 
                            int32_t base);

/*********************************************************************************************
Function Name: pf_str_2_int32
Description  : Convert string to integer with base
Inputs       : const char* str           : the string buffer to be converted. Must be not NULL.
               int32_t base              : it supports 0,2-36 base to be converted.
Outputs      : int32_t* output           : the integer after converted. Must be not NULL.
               return value              :  0  : Success.
                                           <0  : Fail. See errno.
ErrorCodes   : errno                     : errno from strtol().
               PF_EINVAL                 : include invalid character in string buffer.
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
lanqj                  2011-6-24                     create
**********************************************************************************************/
extern int32_t pf_str_2_int32(int32_t *output, const char* str, int32_t base);

/*********************************************************************************************
Function Name: pf_str_2_int64
Description  : Convert string to integer with base
Inputs       : const char* str           : the string buffer to be converted.Must be not NULL.
               int32_t base              : it supports 0,2-36 base to be converted.
Outputs      : int64_t* output           : the integer after converted. Must be not NULL.
               return value              :  0  : Success.
                                           <0  : Fail. See errno.
ErrorCodes   : errno                     : errno from strtoll().
               PF_EINVAL                 : include invalid character in string buffer.
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
lanqj                  2011-6-24                     create
**********************************************************************************************/
extern int32_t pf_str_2_int64(int64_t *output, const char* str, int32_t base); 

/* Convert string to interger */
#define pf_str_2_int(output, str, base)\
	if(sizeof(typeof(*(output))) == sizeof(int32_t))\
		(int32_t)pf_str_2_int32((int32_t*)(output),(const char*)(str),(int32_t)(base));\
	else if(sizeof(typeof(*output)) == sizeof(int64_t))\
		(int32_t)pf_str_2_int64((int64_t*)(output),(const char*)(str),(int32_t)(base));\
	else\
		PF_EINVAL;


/*********************************************************************************************
Function Name: pf_str_trim
Description  : Trim white space chars at the beging and the end of the string.
               eg: white_space_chars="abc" str="bcaxxxb", return value= "xxx"
Inputs       : char* str         : the string buffer to be trimmed
               const char*       : the key string to be trimmed at the beging and the end of the string
Outputs      : return value      : the string buffer head pointer
ErrorCodes   : no
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
lanqj                  2011-6-24                     create
**********************************************************************************************/
extern char* pf_str_trim(char* str, const char* white_space_chars);


/*********************************************************************************************
Function Name: pf_str_2_int_with_unit
Description  : Convert string like : "10.8k" to integer : 110592,
               it supports B,K,M,G,T,P,E.
Inputs       : const char* st            : string buffer to be converted
Outputs      : int63_t *output           : integer after converted
               return value              :  0 : Success.
                                         : <0 : Faild.See errno.
ErrorCodes   : PF_EINVAL                 : 1. unit is not B,K,M,G,T,P,E; 
                                           2. string include invalid charactor.
               errno from strtold.
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
lanqj                  2011-6-24                     create
**********************************************************************************************/
extern int32_t pf_str_2_int_with_unit(int64_t *output, const char* str);


/*********************************************************************************************
Function Name: pf_int_2_str_with_unit
Description  : Convert integer to string with unit. 
               Such as 110592 to "10.8K",it supports B,K,M,G,T,P,E.
Inputs       : int32_t str_len           : the max string buffer length to be output 
                                           after convert.
               int64_t number            : the integer to be converted.
               char unit                 : B,K,M,G,T,P,E,0, 
                                           it will choose best unit when unit is 0,
                                           other character will deal with 'B'.
Outputs      : char* str                 : the string buffer.
               return value              :  0 : success.
                                           <0 : Fail. 
ErrorCodes   : PF_EINVAL                 : 1. str_len < 0; 
                                           2: unit is not B,K,M,G,T,P,E; 
                                           3. str_len is not long enough to store the 
                                              entire integer with unit after convert
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
lanqj                  2011-6-24                     create
**********************************************************************************************/
extern int32_t pf_int_2_str_with_unit(char* str,
									  int32_t str_len,
									  int64_t number,
									  char unit);


/* just rename */
#define pf_printf printf

#define pf_scanf scanf

#define pf_snprintf snprintf

/******************************************************************************************************/

#ifdef __cplusplus
}
#endif
#endif /*#ifndef __PLATFORM_LIB_STRING_H__ */

